//
//  TableViewAnimationKit.m
//  XKStudyDemo
//
//  Created by xukai on 2017/11/28.
//  Copyright © 2017年 xukai. All rights reserved.
//

#import "TableViewAnimationKit.h"

@implementation TableViewAnimationKit

+(void) leftMoveAnimationWith:(UITableView *) tableView
{
    NSArray *cells = tableView.visibleCells;
    for(int i = 0; i<cells.count; i++)
    {
        
        /*
         [UIView animateWithDuration:0.4//动画时长
         delay:0 //动画延迟
         usingSpringWithDamping:0.5 //类似弹簧的震动效果 0~1
         initialSpringVelocity:4  //初始速度
         options:UIViewAnimationOptionCurveLinear//动画过度效果
         */
        
        /*
         transform:
         
         移动:
         cell.transform = CGAffineTransformMakeTranslation() // 每次移动都是相对于最开始的位置
         cell.transform = CGAffineTransformTranslate() //每次移动都是相对于上次位置
         
         缩放:
         cell.transform = CGAffineTransformMakeScale(0.5, 0.5);//每次缩放都是相对于最初的大小
         cell.transform = CGAffineTransformScale(cell.transform, 0.5, 0.5) //每次缩放都是相对于上次的大小
         
         旋转:
         cell.transform = CGAffineTransformMakeRotation(M_PI_4); // 每次旋转都是相对于最初的角度
         cell.transform = CGAffineTransformRotate(cell.transform, M_PI_4);//每次旋转都是相对于现在的角度
         
         还原:
         cell.transform = CGAffineTransformIdentity;
         
         */
        
        CGFloat totalTime = 0.4;
        UITableViewCell *cell = cells[i];
        cell.transform = CGAffineTransformMakeTranslation(-SCREEN_WIDHT, 0);// 初始位置
        
        [UIView animateWithDuration:0.4 delay:i*(totalTime/cells.count) usingSpringWithDamping:0.7 initialSpringVelocity:1/0.7 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
            cell.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
        }];
    }
}

+(void) downAlphaAnimationWith:(UITableView *) tableView
{
    NSArray *cells = tableView.visibleCells;
    for (int i = 0; i<cells.count; i++)
    {
        UITableViewCell *cell = cells[i];
        cell.alpha = 0;
        CGFloat totalTime = 0.4;
        
        [UIView animateWithDuration:0.4 delay:i*(totalTime / cells.count) options:0 animations:^{
            cell.alpha = 1;
        } completion:^(BOOL finished) {
            
        }];
    }
}

+(void) downMoveAnimationWith:(UITableView *) tableView
{
    NSArray *cells = tableView.visibleCells;
    for (int i = 0; i<cells.count; i++)
    {
        UITableViewCell *cell = cells[i];
        CGFloat totalTime = 0.8;
        cell.transform = CGAffineTransformMakeTranslation(0, -SCREEN_HEIGHT);
        
        [UIView animateWithDuration:0.3 delay:(cells.count - i)*(totalTime/cells.count) options:0 animations:^{
            cell.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
        }];
    }
}

+(void) leftRightMoveAnimationWith:(UITableView *) tableView
{
    NSArray *cells = tableView.visibleCells;
    for(int i = 0; i<cells.count; i++)
    {
        CGFloat totalTime = 0.4;
        UITableViewCell *cell = cells[i];
        if(i % 2 == 0)
        {
            cell.transform = CGAffineTransformMakeTranslation(-SCREEN_WIDHT, 0);// 初始位置
        }
        else
        {
            cell.transform = CGAffineTransformMakeTranslation(SCREEN_WIDHT, 0);// 初始位置

        }
        [UIView animateWithDuration:0.4 delay:i*(totalTime/cells.count) usingSpringWithDamping:0.7 initialSpringVelocity:1/0.7 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
            cell.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
        }];
    }
}

+(void) rotateAnimatioWith:(UITableView *) tableView
{
    NSArray *cells = tableView.visibleCells;
    for(int i = 0; i<cells.count; i++)
    {
        CGFloat totalTime = 0.7;
        UITableViewCell *cell = cells[i];
        cell.layer.transform = CATransform3DMakeRotation(M_PI, 1, 0, 0);
        cell.alpha = 0;
        [UIView animateWithDuration:0.4 delay:i*(totalTime/cells.count) options:0 animations:^{
            cell.layer.transform = CATransform3DIdentity;
            cell.alpha = 1;
        } completion:^(BOOL finished) {
            
        }];
    }
}

+(void) upMoveAnimationWith:(UITableView *) tableView
{
    NSArray *cells = tableView.visibleCells;
    for (int i = 0; i<cells.count; i++)
    {
        UITableViewCell *cell = cells[i];
        CGFloat totalTime = 0.8;
        cell.transform = CGAffineTransformMakeTranslation(0, SCREEN_HEIGHT);
        
        [UIView animateWithDuration:0.3 delay:i*(totalTime/cells.count) options:0 animations:^{
            cell.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
        }];
    }
}

+(void) layDownAnimationWith:(UITableView *) tableView
{
    NSArray *cells = tableView.visibleCells;
    NSTimeInterval totalTime = 1.0;
    for (int i = 0; i<cells.count; i++)
    {

        UITableViewCell *cell = [cells objectAtIndex:i];
        cell.transform = CGAffineTransformMakeTranslation(0, -SCREEN_HEIGHT);// 初始位置
        [UIView animateWithDuration:0.4 delay:i*(totalTime/cells.count) usingSpringWithDamping:0.7 initialSpringVelocity:1/0.7 options:(UIViewAnimationOptionCurveEaseIn) animations:^{
            cell.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
        }];
    }
}

+ (void)shrinkToTopAnimationWithTableView:(UITableView *)tableView
{
    NSArray *cells = tableView.visibleCells;
    NSTimeInterval totalTime = 0.5;
    for (int i = 0; i<cells.count; i++)
    {
        UITableViewCell *cell = [cells objectAtIndex:i];
        cell.transform = CGAffineTransformMakeTranslation(0, cell.top + 50);
        [UIView animateWithDuration:totalTime animations:^{
            cell.transform = CGAffineTransformIdentity;
        }];

    }
}



@end
